<script setup lang="ts">
  import { ref } from 'vue';
  import { TableInstance, Message } from '@arco-design/web-vue';
  import useLoading from '@/hooks/loading';
  import { useUserStore } from '@/store';
  const userStore = useUserStore();
  defineOptions({
    name: 'companylist',
  });
  import Add from './add/index.vue';
  import Addzhiz from './add/addzhiz.vue';
  import Addbiang from './add/addbiang.vue';
  import Edit from './edit/index.vue';
  import { deletePolicy, getAllpolicy, getstalist } from '@/api/policy';
  import { getindustry, getpolicy_direction } from '@/api/dataCenter';
  import { deletecompany, getcompanylist } from '@/api/enterlibery';
  const { loading, setLoading } = useLoading(false);
  const proTableRef = ref<TableInstance | null>(null);
  // 表单类型
  function getTotal() {
    setLoading(true);
  }
  const formType = ref([
    {
      label: '企业名称',
      name: 'gsmc',
      fieldProps: {
        span: 8,
      },
    },
    {
      name: 'province_id',
      label: '省份',
      type: 'a-select',
      fieldProps: {
        span: 8,
        allowSearch: true,
        fieldNames: { value: 'id', label: 'name' },
      },
      selectOptions: userStore.allProvice,
    },
  ]);
  const visible = ref(false);
  const visible1 = ref(false);
  const visible2 = ref(false);
  const visible3 = ref(false);
  const editParam = ref();
  const tableType = [
    { dataIndex: 'company_id', slotName: 'id' },
    {
      title: '企业名称',
      dataIndex: 'gsmc',
    },
    {
      title: '法人代表',
      dataIndex: 'frdb',
      align: 'center',
    },
    {
      title: '注册地址',
      dataIndex: 'zcdz',
      align: 'center',
    },

    {
      title: '成立时间',
      dataIndex: 'clrq',
      align: 'center',
    },
    {
      title: '统一社会信用代码',
      dataIndex: 'tyshxydm',
    },
    {
      title: '操作',
      width: 130,
      fixed: 'right',
      slotName: 'operations',
      menus: [
        {
          name: '修改',
          onClick: (record: any) => {
            visible1.value = true;
            editParam.value = record.company_id;
          },
        },
        {
          name: '删除',
          onClick: (record: any) => {
            deletecompany({
              company_ids: [record.company_id],
            }).then((res) => {
              proTableRef.value?.setRefresh();
              Message.success('删除成功！');
              request();
            });
          },
        },
        {
          name: '企业资质',
          onClick: (record: any) => {
            visible2.value = true;
            editParam.value = record.company_id;
          },
        },
        {
          name: '企业变更',
          onClick: (record: any) => {
            visible3.value = true;
            editParam.value = record.company_id;
          },
        },
      ],
    },
  ];

  const totoalpolicy = ref();
  const industryAll = ref();
  const directionAll = ref();
  onMounted(() => {
    // 获取政策行业和政策方向
    Promise.all([
      getindustry({ page_size: 10000 }),
      getpolicy_direction({ page_size: 10000 }),
    ]).then(([industryRes, policyRes]) => {
      industryAll.value = industryRes.data.list;
      directionAll.value = policyRes.data.list;
    });
    getTotal();
  });

  async function request(params) {
    const res = await getcompanylist(params);
    return {
      data: res.data,
    };
  }
</script>

<template>
  <a-row :style="{ marginBottom: '20px', padding: '10px' }" class="box">
    <a-row style="width: 100%">
      <a-col :span="24">
        <pro-table
          style="margin-top: 30px"
          ref="proTableRef"
          :formType="formType"
          :request="request"
          :tableType="tableType"
        >
          <template #btns>
            <a-button
              type="primary"
              @click="
                () => {
                  visible = true;
                }
              "
            >
              <template #icon> <icon-plus /> </template>
              <template #default>添加企业</template>
            </a-button>
          </template>
          <template #options>
            <Add
              v-if="visible"
              v-model:visible="visible"
              :params="editParam"
              :industryAll="industryAll"
              :directionAll="directionAll"
              @refresh="
                () => {
                  getTotal();
                  $refs.proTableRef.setRefresh();
                }
              "
            />
            <Edit
              v-if="visible1"
              v-model:visible="visible1"
              :params="editParam"
              @refresh="
                () => {
                  $refs.proTableRef.setRefresh();
                }
              "
            />
            <Addzhiz
              v-if="visible2"
              v-model:visible="visible2"
              :params="editParam"
              @refresh="
                () => {
                  $refs.proTableRef.setRefresh();
                }
              "
            />
            <Addbiang
              v-if="visible3"
              v-model:visible="visible3"
              :params="editParam"
              @refresh="
                () => {
                  $refs.proTableRef.setRefresh();
                }
              "
            />
          </template>
        </pro-table>
      </a-col>
    </a-row>
  </a-row>
</template>

<style scoped lang="less">
  .box {
    width: 100%;
    height: 100%;
    background: #ffffff;
    padding: 10px;
    box-sizing: border-box;
    .countBox {
      width: 100%;
      padding: 20px !important;
      box-sizing: border-box;
      //background: linear-gradient(56deg, #f6f8fb, #fafcfd 98%);
      //border: 1px solid #ecf1fd;
      //box-shadow: 0 0 16px #7491c924;
    }
  }
</style>
